package com.journaldev.hibernate.main;

import java.util.List;

import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;

import com.journaldev.hibernate.util.HibernateUtil;

public class HQLExamples2 {

	@SuppressWarnings("unchecked")
	public static void main(String[] args) {
		
		//Prep work
		SessionFactory sessionFactory = HibernateUtil.getSessionFactory();
		Session session = sessionFactory.getCurrentSession();
		Query query  = null;
		Transaction tx = session.beginTransaction();
	/*	
		//Get All Employees
		Query query = session.createQuery("from Employee");
		List<Employee> empList = query.list();
		for(Employee emp : empList){
			System.out.println("List of Employees::"+emp.getId()+","+emp.getAddresses().size());
		}*/
		/**
		 * SELECT
	address0_.zipcode AS col_0_0_,
	employee3_.emp_name AS col_1_0_,
	organs5_.ogan_name AS col_2_0_
FROM
	hibernate_test.address address0_
LEFT OUTER JOIN hibernate_test.employee employee1_ ON address0_.emp_id = employee1_.id
LEFT OUTER JOIN hibernate_test.organs organs2_ ON employee1_.organ_id = organs2_.id,
 hibernate_test.employee employee3_,
 hibernate_test.organs organs5_
WHERE
	address0_.emp_id = employee3_.id
AND employee3_.organ_id = organs5_.id
		 * 
		 */
/*		query = session.createQuery("select a.zipcode,a.employee.empName,a.employee.organs.oganName  from Address a left join a.employee e"
				+ "		left join e.organs o");
		List<Object> xx = query.list();
		for(Object x : xx){
			System.out.println(x);
		}*/
		
		
		/**
		 * SELECT
	address0_.zipcode AS col_0_0_,
	employee1_.emp_name AS col_1_0_,
	organs2_.ogan_name AS col_2_0_
FROM
	hibernate_test.address address0_
LEFT OUTER JOIN hibernate_test.employee employee1_ ON address0_.emp_id = employee1_.id
LEFT OUTER JOIN hibernate_test.organs organs2_ ON employee1_.organ_id = organs2_.id
		 * 
		 */
		query = session.createQuery("select a.zipcode,e.empName,o.oganName  from Address a left join a.employee e"
				+ "		left join e.organs o");
		List<Object> yy = query.list();
		for(Object y : yy){
			System.out.println(y);
		}
		
		//rolling back to save the test data
		tx.rollback();
		
		//closing hibernate resources
		sessionFactory.close();
	}

}
